package com.hnichr.crm.biz.business.service.impl;

import com.hnichr.crm.common.business.entity.CrmBusiness;
import com.hnichr.crm.common.business.service.ICrmBusinessService;
import com.mxpioframework.filestorage.entity.MxpioFileInfo;
import com.mxpioframework.filestorage.service.FileStorageService;
import com.mxpioframework.jpa.JpaUtil;
import com.mxpioframework.jpa.initiator.JpaUtilAble;
import com.mxpioframework.jpa.query.Criteria;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.print.DocFlavor;
import java.util.ArrayList;
import java.util.List;

/**
 * 商机Service业务层处理
 *
 * @author Cheng
 * @date 2022-11-24
 */
@Service
public class CrmBusinessServiceImpl implements ICrmBusinessService, JpaUtilAble {

    @Autowired
    private FileStorageService fileStorageService;

    @Override
    public void afterPropertiesSet(ApplicationContext applicationContext) {

    }

    @Override
    @Transactional(readOnly = true)
    public Page<CrmBusiness> page(Criteria criteria, Pageable pageable) {
        Page<CrmBusiness> page = JpaUtil.linq(CrmBusiness.class).where(criteria).paging(pageable);
        if(CollectionUtils.isNotEmpty(page.getContent())){
            for(CrmBusiness crmBusiness:page.getContent()){
                getFileNames(crmBusiness);
            }
        }
        return page;
    }

    @Override
    @Transactional(readOnly = true)
    public CrmBusiness getById(String id) {
        CrmBusiness crmBusiness =  JpaUtil.linq(CrmBusiness.class).idEqual(id).findOne();
        getFileNames(crmBusiness);
        return crmBusiness;
    }

    @Override
    @Transactional(readOnly = false)
    public void save(CrmBusiness crmBusiness) {
        JpaUtil.save(crmBusiness);
    }

    @Override
    @Transactional(readOnly = false)
    public void update(CrmBusiness crmBusiness) {
        JpaUtil.update(crmBusiness);
    }

    @Override
    @Transactional(readOnly = false)
    public int delete(String id) {
        return JpaUtil.lind(CrmBusiness.class).equal("id", id).delete();
    }

    private void getFileNames(CrmBusiness crmBusiness){
        if(crmBusiness!=null&& StringUtils.isNotBlank(crmBusiness.getFileList())){
            List<String> fileNames = new ArrayList<>();
            String [] fileNos = crmBusiness.getFileList().split(",");
            for(String fileNo:fileNos){
                try{
                    MxpioFileInfo fileInfo = fileStorageService.get(fileNo);
                    fileNames.add(fileInfo.getFileName());
                }
                catch (Exception e){
                    fileNames.add(fileNo);
                }
            }
            crmBusiness.setFileNames(String.join(",",fileNames));
        }
    }
}
